package com.candoo.oil.scan;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ExpandedMenuView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

    private final static String SCAN_ACTION = "scan.rcv.message";

    private String barcodeStr;



    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            //click_login(null);
            //return;
            byte[] barocode = intent.getByteArrayExtra("barocode");
            int barocodelen = intent.getIntExtra("length", 0);
            byte temp = intent.getByteExtra("barcodeType", (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            barcodeStr = new String(barocode, 0, barocodelen);
            // showScanResult.append("广播输出：");
            // showScanResult.append(barcodeStr);
            // showScanResult.append("\n");
           // showScanResult.setText(barcodeStr);
            //{"url":"http://sale.nihao99.com/AgentCenter/Test/Interface","account":"Tinder","sessionid":"11223344"}
            Log.e("Scan_finish",barcodeStr);
            try{
                JSONObject js=new JSONObject(barcodeStr);
Config.Url=js.getString("url");
                Config.Account=js.getString("account");
                Config.SessionId=js.getString("sessionid");



                click_login(null);
            }
            catch (Exception e)
            {
                Toast.makeText(getApplicationContext(), "扫描解析错误",
                        Toast.LENGTH_SHORT).show();
            }
            finally {

            }



            Config.sm.stopScan();
        }

    };





    private P_Response_Login _info;
    private ProgressDialog _pd;

    private void Login_Result()
    {
        _pd.cancel();
        if(_info.flag>=0)
        {
            Config.isLogin=true;
            click_close(null);
        }
        else
        {
            Toast.makeText(getApplicationContext(), _info.msg,
                    Toast.LENGTH_SHORT).show();
        }


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.v("info","登录处理界面创建");
    }

    public void click_login(View view)
    {
      //  Config.isLogin=true;
       // click_close(null);
      //  return;

        _pd= ProgressDialog.show(this,"","正在登录中...");
        P_Response_Login r = new P_Response_Login();
        r.flag = 0;
        r.msg = "ok";
        _info=r;
        Login_Result();
/*
        new Thread(){
            @Override
            public void run()
            {
                _info=     httpClientClass.Login(Config.Account,Config.SessionId);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Login_Result();

                    }
                });
            }
        }.start();*/




    }



    @Override
    protected void onResume() {
        //恢复
        super.onResume();

            IntentFilter filter = new IntentFilter();
            filter.addAction(SCAN_ACTION);
            registerReceiver(mScanReceiver, filter);
            // ScanClass.Start();




        Log.e("info","登录处理界面onResume");
        // IntentFilter filter = new IntentFilter();
        //  filter.addAction(SCAN_ACTION);
        //  registerReceiver(mScanReceiver, filter);
    }
    @Override
    protected void onPause() {
        //暂停
        super.onPause();
        Config.sm.stopScan();
        unregisterReceiver(mScanReceiver);
        //Config.isLogining=false;
        click_close(null);
       // click_close(null);
       // Config.sm.stopScan();
       // unregisterReceiver(mScanReceiver);
        Log.e("info","登录处理界面onPause");
        //if(sm != null) {
        //    sm.stopScan();
        // }
        // unregisterReceiver(mScanReceiver);
    }

    public void click_close(View view) {
        Config.isLogining=false;

        finish(); //退出后台线程,以及销毁静态变量
       // System.exit(0);
       // finish();
    }
}
